﻿using BigMonitoringWarningApi.Model.ApiResult;
using GYCTOAManagementSystemNewApi.Model;
using GYCTOAManagementSystemNewApi.Model.Entity;
using GYCTOAManagementSystemNewApi.Model.Enum.PayrollForm;
using GYCTOAManagementSystemNewApi.Model.Request;
using GYCTOAManagementSystemNewApi.Model.Response;
using GYCTOAManagementSystemNewApi.Model.Tool;
using GYCTOAManagementSystemNewApi.Public;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.SqlServer.Server;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using NuGet.Packaging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using GYCTOAManagementSystemNewApi.Tool;
using GYCTOAManagementSystemNewApi.Model.Enum.Livingexpenses;

namespace GYCTOAManagementSystemNewApi.Controllers.Socialinsurance
{
    /// <summary>
    ///生活费
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class SocialinsuranceController : Controller
    {
        private readonly ILogger<DocumentController> _logger;
        DBContext _db;
        PublicTool _publictool;
        public SocialinsuranceController(ILogger<DocumentController> logger, DBContext db)
        {
            _logger = logger;
            _db = db;
            _publictool = new PublicTool(_db);
        }

        [HttpPost("AddSocialinsurance")]
        public ApiResult AddSocialinsurance(AddSocialinsuranceRequset requset)
        {
            try
            {
                List<string> userName = new();
                var author = HttpContext.Request.Headers["Authorization"].ToString().Split(' ')[1];
                var userid = Convert.ToInt32(TokenManage.GetTokenValues(author)[0].Value);
                var  Users=_db.SocialinsuranceData.Where(x=>x.SettlementNumber==requset.SocialinsurancetList.FirstOrDefault().SettlementNumber).ToList();
                foreach (var item in requset.SocialinsurancetList)
                {
                    if (Users.FirstOrDefault(x => x.UserId == item.UserId) != null)
                    {
                        var userfir = _db.Users.FirstOrDefault(x => x.Id == item.UserId);
                        userName.Add(($"{userfir.UserName}-{userfir.UserNumber}"));
                    }
                    else { 
                        item.OperatId = userid;
                        item.OperatDate = DateTime.Now;
                        _db.SocialinsuranceData.Add(item);
                    }
                }
                _db.SaveChanges();
                if (userName.Any())
                {
                    var value = string.Join(",", userName) + $"等人员在{requset}批号已添加";
                    return ApiResult.False(value);
                }
                return ApiResult.True();
            }
            catch (Exception e)
            {
                return ApiResult.False(e.Message);

            }
        }


        /// <summary>
        /// 社保修改
        /// </summary>
        /// <param name="requset"></param>
        /// <returns></returns>
        [HttpPost("UpSocialinsurance")]
        public ApiResult UpSocialinsurance(AddSocialinsuranceRequset requset)
        {
            try
            {
                List<string> userName = new();
                var author = HttpContext.Request.Headers["Authorization"].ToString().Split(' ')[1];
                var userid = Convert.ToInt32(TokenManage.GetTokenValues(author)[0].Value);
                var Users = _db.SocialinsuranceData.Where(x => x.SettlementNumber == requset.SocialinsurancetList.FirstOrDefault().SettlementNumber).ToList();
                foreach (var item in requset.SocialinsurancetList)
                {
                    var  socialin=Users.FirstOrDefault(x => x.UserId == item.UserId);
                    socialin.SettlementNumber = item.SettlementNumber;
                    socialin.Socialinsurance=item.Socialinsurance;
                    socialin.OperatDate = DateTime.Now;
                    socialin.OperatId = userid;
                    _db.SocialinsuranceData.Update(socialin);
                }
                _db.SaveChanges();
                
                return ApiResult.True();
            }
            catch (Exception e)
            {
                return ApiResult.False(e.Message);

            }
        }
    }
}
